package com.example.javaproject.jvm;

/**
 * Description: 引用计数法 不能处理存活引用的问题
 * <br/>Author:  tianjinyong
 * <br/>Date:    2020/5/11 10:43 PM
 */
public class ReferenceCountingGc {

    public Object instance = null;
    private static final int _1M = 1024 * 1024;

    /**
     * 这个成员变量的唯一意义就是占用内存，以边在Gc日志中看清楚是否被回收过
     */
    private byte[] bigSize = new byte[2 * _1M];

    public static void testGc() {
        ReferenceCountingGc objA = new ReferenceCountingGc();
        ReferenceCountingGc objB = new ReferenceCountingGc();
        objA.instance = objB;
        objB.instance = objA;

        objA = null;
        objB = null;

        //假设在这里发生gc，objA和objB是否被回收
        System.gc();
    }

    public static void main(String[] args) {
        testGc();
    }
}
